package com.example.leetcode.common;


/**
 * @author zhanpengguo
 * @date 2019-08-16 11:34
 */
public class ListOperate {

    public static ListNode create(int[] ints) {
        if (ints == null || ints.length == 0) {
            return null;
        }
        ListNode linkNode = new ListNode(ints[0]);
        if (ints.length == 1) {
            return linkNode;
        }
        for (int i = 1; i < ints.length; i++) {
            insert(linkNode, ints[i]);
        }
        return linkNode;
    }

    public static void insert(ListNode linkNode, int node) {
        while (linkNode.next != null) {
            linkNode = linkNode.next;
        }
        linkNode.next = new ListNode(node);
    }

    public static void show(ListNode linkNode) {
        System.out.print(linkNode.val);
        System.out.print("->");
        while (linkNode.next != null) {
            linkNode = linkNode.next;
            System.out.print(linkNode.val);
            System.out.print("->");
        }
        System.out.println("null");
    }

    public static void main(String[] args) {
        int[] ints = {5, 6, 7};
        ListNode linkNode = create(ints);
        show(linkNode);
        System.out.println();
        rever(linkNode);
    }

    public static void rever(ListNode listNode) {
        if (listNode != null) {
            rever(listNode.next);
            System.out.println(listNode.val);
        }
//        if (listNode.next != null) {
//            listNode = listNode.next;
//            rever(listNode);
//        }
//        System.out.println(listNode.val);
    }
}
